//
//  ShoppingCollectionViewCell.m
//  AmountBody
//
//  Created by smm on 2017/12/20.
//  Copyright © 2017年 RCMTM. All rights reserved.
//

#import "ShoppingCollectionViewCell.h"
#define itemHeight 30
@implementation ShoppingCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        //        self.contentView.backgroundColor = [UIColor redColor];
        // 用约束来初始化控件:
        self.textLabel = [[UILabel alloc] init];
        _textLabel.textAlignment =NSTextAlignmentCenter;
        _textLabel.textColor = QianZi_color;
        _textLabel.font = Cell_Little_Font;
#pragma mark — 如果使用CGRectMake来布局,是需要在preferredLayoutAttributesFittingAttributes方法中去修改textlabel的frame的
        self.textLabel.frame = CGRectMake(0, 0, frame.size.width, itemHeight);
        self.tagView = [[HJShapedImageView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, itemHeight)];
        self.tagView.backgroundColor = [UIColor clearColor];
        self.image = [[UIImageView alloc]init];
        [self.tagView addSubview:_textLabel];
        [self.tagView addSubview:_image];
        [self.contentView addSubview:self.tagView];
    }
    return self;
}
//
//
//#pragma mark — 实现自适应文字宽度的关键步骤:item的layoutAttributes
- (UICollectionViewLayoutAttributes *)preferredLayoutAttributesFittingAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes{

    UICollectionViewLayoutAttributes *attributes = [super preferredLayoutAttributesFittingAttributes:layoutAttributes];

    CGRect frame = [_textLabel.text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, _textLabel.frame.size.height) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:[NSDictionary dictionaryWithObjectsAndKeys:_textLabel.font,NSFontAttributeName, nil] context:nil];

    frame.size.height = itemHeight;
    frame.size.width = frame.size.width+20;
    attributes.frame = frame;
    // 如果你cell上的子控件不是用约束来创建的,那么这边必须也要去修改cell上控件的frame才行
    self.textLabel.frame = CGRectMake(0, 0, attributes.frame.size.width, itemHeight);
//    self.tagView.frame = CGRectMake(0, 0, attributes.frame.size.width, itemHeight);
    return attributes;
}
-(void)setCellForTetiWithModel:(ShoppingCollectionModel *)model
{
    self.textLabel.text = model.title;
    self.textLabel.textAlignment = NSTextAlignmentCenter;
    self.textLabel.font = Cell_Little_Font;
    self.tagView.frame = CGRectMake(0, 0, model.size.width, itemHeight);
    self.tagView.image = [UIImage imageWithColor:RGBColor(23, 178, 67)];
    self.image.hidden = YES;
}
-(void)setCellWithModel:(ShoppingCollectionModel*)model
{
    CGFloat imageW = 16;
    CGFloat imageH = 16;
    self.textLabel.text = model.title;
    if (model.is_Choose) {
        self.textLabel.textColor = RGBColorAlpha(255, 66, 89, 1);
        [self setBianKuangAndColor:RGBColorAlpha(255, 66, 89, 1)];
    }else{
        self.textLabel.textColor = QianZi_color;
        [self setBianKuangAndColor:Line_color];
    }
    
    self.textLabel.backgroundColor = [UIColor whiteColor];
    self.textLabel.textAlignment = NSTextAlignmentCenter;
    self.textLabel.font = Cell_Little_Font;
    self.tagView.frame = CGRectMake(0, 0, model.size.width, itemHeight);
    self.tagView.image = [UIImage imageWithColor:RGBColor(23, 178, 67)];
    self.image.frame = CGRectMake(model.size.width-12-imageW, (self.frame.size.height-imageH)/2.0, imageW, imageH);
    self.image.hidden = NO;
    if (_isTag) {
        [self loadCellForTagView];
    }else{
        [self loadCellForNomalView];
    }
}

-(void)loadCellForTagView
{
    self.textLabel.backgroundColor = [UIColor clearColor];
    self.textLabel.textColor =[UIColor whiteColor];
    [self.tagView bringSubviewToFront:self.textLabel];
    [self.tagView bringSubviewToFront:self.image];
    [self setBianKuangAndColor:RGBColorAlpha(255, 66, 89, 0)];
    [self setYuanJiaoForRadius:8.0 andMasks:YES];
}
-(void)loadCellForNomalView
{
    self.tagView.image = [UIImage imageNamed:@""];
    self.image.image = [UIImage imageNamed:@""];
    
    [self setYuanJiaoForRadius:8.0 andMasks:YES];
}
-(void)setIsChoose:(BOOL)isChoose
{
    if (self.isTag) {
        if (isChoose) {
            
            self.image.image = [UIImage imageNamed:@"c_gouxuan"];
        }else{
            self.image.image = [UIImage imageNamed:@""];
        }
    }else{
        if (isChoose) {
            self.textLabel.textColor = RGBColorAlpha(255, 66, 89, 1);
            [self setBianKuangAndColor:RGBColorAlpha(255, 66, 89, 1)];
            
        }else{
            self.textLabel.textColor = QianZi_color;
            [self setBianKuangAndColor:Line_color];
            
        }
    }
    
}
@end
